package com.chen.base.utils;

import cn.hutool.core.util.StrUtil;
import com.chen.base.feign.RemoteFrameworkService;
import com.chen.root.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.chen.root.constant.CacheKeyCons.COMMON_CONFIG_KEY;

/**
 * @author: ChenJinLong
 * @DATE: 2024/7/18
 **/
@Slf4j
@Component
public class CommonConfigUtil {

    @Resource
    private RemoteFrameworkService remoteFrameworkService;

    /**
     * 查询公共配置
     * @param name 配置名称
     * @return 配置值
     */
    @Cacheable(cacheNames = COMMON_CONFIG_KEY, key = "#name")
    public String getValueByName(String name){
        if (StrUtil.isBlank(name)){
            return null;
        }
        R<String> r = remoteFrameworkService.getCommonConfigValueByName(name);
        return r.getData();
    }

    public static String getValue(String name){
        CommonConfigUtil configUtil = SpringContextHolder.getBean(CommonConfigUtil.class);
        return configUtil.getValueByName(name);
    }
}
